package com.weimob.mcloud.wechatwork.customer.mq.userPermission.handler;

import com.alibaba.fastjson.JSON;
import com.weimob.mcloud.wechatwork.customer.mq.userPermission.dto.EmployeeVidsDTO;
import com.weimob.mcloud.wechatwork.customer.mq.userPermission.dto.PermissionChangeMsgDTO;
import com.weimob.zipkin.ZipkinContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 处理员工数据隔离权限数据
 *
 * @Author LeoLee
 * @Date 2022/8/22 16:30
 * @Version 1.0
 */
@Slf4j
public abstract class AbstractUserPermissionChangeHandler {

    /**
     * 处理变更消息
     * @Author LeoLee
     * @Date 17:11 2022/8/22
     * @param message
     * 
     */
    public void userPermissionChange(String message) {
        log.info("userPermissionChange message:{}", message);
        //解析变更消息
        PermissionChangeMsgDTO permissionChangeMsgDTO = parseMessage(message);
        if (null == permissionChangeMsgDTO) {
            log.info("null user permission change msg, globalTicket:{}", ZipkinContext.getContext().getGlobalTicket());
            return;
        }
        //获取消息中需要变更的员工和其权限vidList数据
        List<? extends EmployeeVidsDTO> allEmployee = getAllEmployee(permissionChangeMsgDTO);
        if (CollectionUtils.isNotEmpty(allEmployee)) {
            //修改权限数据
            updatePermission(allEmployee, permissionChangeMsgDTO.getType());
        }
    }

    /**
     * 格式化消息为对象（消息内容为空，则返回null）
     * @Author LeoLee
     * @Date 16:42 2022/8/22
     * @param message
     * @return com.weimob.mcloud.wechatwork.customer.mq.userPermission.dto.PermissionChangeMsgDTO
     */
    private PermissionChangeMsgDTO parseMessage(String message) {
        if (StringUtils.isEmpty(message)) {
            return null;
        }
        PermissionChangeMsgDTO permissionChangeMsgDTO = JSON.parseObject(message, PermissionChangeMsgDTO.class);
        return permissionChangeMsgDTO;
    }

    /**
     * 获取所有涉及权限变更的员工
     * @Author LeoLee
     * @Date 17:22 2022/8/22
     * 
     * @param permissionChangeMsgDTO
     * 
     */
    protected abstract List<? extends EmployeeVidsDTO> getAllEmployee(PermissionChangeMsgDTO permissionChangeMsgDTO);

    /**
     * 根据员工与vid的对应关系修改业务权限字段
     * @Author LeoLee
     * @Date 16:11 2022/8/23
     */
    protected abstract void updatePermission(List<? extends EmployeeVidsDTO> employeeVidsDTOS, Integer msgType);

    /**
     * 消息类型
     * @Author LeoLee
     * @Date 17:05 2022/8/22
     */
    protected enum PermissionChangeTypeEnum {

        /**
         * 枚举列
         */
        EMPLOYEE(1,"员工"),

        DEPARTMENT(2,"部门"),

        ALL_VID_CHANGE(3,"全量消息变更处理");

        /**
         * 枚举值
         */
        private Integer code;

        /**
         * 枚举描述
         */
        private String text;


        PermissionChangeTypeEnum(Integer code, String text) {
            this.code = code;
            this.text = text;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public static PermissionChangeTypeEnum getEnumByCode(Integer code) {
            if (null == code) {
                return null;
            }
            for (PermissionChangeTypeEnum e : PermissionChangeTypeEnum.values()) {
                if (e.getCode().intValue() == code.intValue()) {
                    return e;
                }
            }
            return null;
        }
    }

}
